package com.ysstech.kettle.controller;


import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ysstech.common.annotation.YssLog;
import com.ysstech.common.annotation.YssPermissions;
import com.ysstech.common.controller.BaseController;
import com.ysstech.common.exception.BusinessException;
import com.ysstech.common.util.DateUtil;
import com.ysstech.common.util.JsonUtil;
import com.ysstech.common.util.resultutil.RestResult;
import com.ysstech.common.util.resultutil.ResultGenerator;
import com.ysstech.kettle.entity.JobDutiesTask;
import com.ysstech.kettle.service.JobDutiesTaskService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 采集任务和作业任务关联关系表 前端控制器
 * </p>
 *
 * @author lishuangliang
 * @since 2022-10-25
 */
@Controller
@Slf4j
@Api(tags = "采集任务和采集作业关联关系管理api")
@RequestMapping("/jobDutiesTask")
public class JobDutiesTaskController extends BaseController {

    @Autowired
    private JobDutiesTaskService jobDutiesTaskService;


    /**
     * 新增采集任务的方法
     *
     * @param jobDutiesTask
     * @return
     */
    @YssPermissions(value = {"kettle:job:duties"})
    @ApiOperation(value = "新增采集任务信息")
    @YssLog(module = "采集任务管理", remark = "新增采集任务信息")
    @RequestMapping(value = "/addJobDutiesTask")
    @ResponseBody
    public RestResult addJobDutiesTask(String jobDutiesTask) throws Exception {
        if (StringUtils.isBlank(jobDutiesTask)) {
            throw new BusinessException("获取删除采集任务信息失败，请稍后重试！");
        }
        List<JobDutiesTask> list = JSONArray.parseArray(jobDutiesTask, JobDutiesTask.class);
        if (CollectionUtils.isEmpty(list)) {
            throw new BusinessException("获取删除采集任务信息失败，请稍后重试！");
        }
        List<JobDutiesTask> dutiesTaskList = new ArrayList<>();
        for (JobDutiesTask dutiesTask : list) {
            if (null == dutiesTask || StringUtils.isBlank(dutiesTask.getDutiesId()) || StringUtils.isBlank(dutiesTask.getTaskId())) {
                continue;
            }
            dutiesTask.setId(DateUtil.generateUUID());
            dutiesTask.setAddTime(LocalDateTime.now());
            dutiesTaskList.add(dutiesTask);
        }
        if (CollectionUtils.isEmpty(dutiesTaskList)) {
            throw new BusinessException("获取删除采集任务信息失败，请稍后重试！");
        }
        QueryWrapper<JobDutiesTask> wrapper = new QueryWrapper<>();
        wrapper.eq("DUTIES_ID", dutiesTaskList.get(0).getDutiesId());
        jobDutiesTaskService.remove(wrapper);
        jobDutiesTaskService.saveBatch(dutiesTaskList);
        return ResultGenerator.getSuccessResult("新增采集任务信息成功！");
    }

}
